package com.sdy.approval.biz.service.impl;

import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sdy.approval.biz.constants.MqConstants;
import com.sdy.approval.biz.model.TpApproveFlow;
import com.sdy.approval.biz.model.TpOrder;
import com.sdy.approval.biz.mapper.TpOrderMapper;
import com.sdy.approval.biz.service.TpApproveFlowService;
import com.sdy.approval.biz.service.TpOrderService;
import com.sdy.common.model.BizException;
import com.sdy.common.utils.Assert;
import com.sdy.mq.service.MqProducerService;
import com.sdy.mvc.service.impl.BaseServiceImpl;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 资源申请表 服务实现类
 * </p>
 *
 * @author cgj
 * @since 2019-08-09
 */
@Slf4j
@Service
public class TpOrderServiceImpl extends BaseServiceImpl<TpOrder> implements TpOrderService {
	@Autowired
	private TpOrderMapper tpOrderMapper;
	@Autowired
	private TpOrderService tpOrderService;
	@Autowired
	private MqProducerService mqProducerService;
	@Autowired
	private TpApproveFlowService tpApproveFlowService;

	/**
	 * 撤回订单和生成的流程节点数据
	 * @param applyNum
	 * @param date
	 * @param userId
	 * @return
	 * @throws BizException
	 */
	@Override
	public Boolean withdrawOrderAndFlows(String applyNum, Date date, Integer userId) throws BizException {
		// ------------撤回TpOrder----------------------
		TpOrder order = tpOrderService.getOne(Wrappers.<TpOrder>lambdaQuery().eq(TpOrder::getApplicationNum, applyNum)
				.eq(TpOrder::getOrderAvailable, 1));
		// 根据id更新状态to已撤回
		TpOrder tpOrder = order.setGsOrderState(5).setModifyTime(date).setCancelUserId(userId);
		Boolean wdto = tpOrderService.updateById(tpOrder);
		Assert.notTrue(wdto, "(云订单)撤回TpOrder内的信息失败!");
		// 撤回生成的流程节点数据
		tpApproveFlowService.withdrawFlows(applyNum, date, userId);

		return true;
	}

	@Override
	public Boolean sendMq(String applyNum, String resId) throws Exception {
		LambdaQueryWrapper<TpOrder> superEndWrapper = new LambdaQueryWrapper<>();
		superEndWrapper.eq(TpOrder::getApplicationNum, applyNum)
				.eq(TpOrder::getOrderAvailable, 1);
		TpOrder tp = tpOrderService.getOne(superEndWrapper);
		String data = JSON.toJSONString(tp);
		if (resId.contains("[政务超市]")) {
			mqProducerService.send(MqConstants.Tags.TAG_UPDATENODEAPPROVALRESULTS, data);
		} else if (resId.contains("[资源目录]")) {
			mqProducerService.send(MqConstants.Tags.TAG_RESDIRAPPROVALRESULTS, data);
		}
		log.info("审批成功+成功发送mq");
		return true;
	}

	@Override
	public List<TpOrder> selectAllTpOrderListNotSubmit() {
		return tpOrderMapper.findAllTpOrderListNotSubmit();
	}

	@Override
	public List<TpOrder> selectAllTpOrderListSubmit() {
		return tpOrderMapper.findAllTpOrderListSubmit();
	}

	@Override
	public List<TpOrder> selectTpOrderByConditionNotApproveds(TpApproveFlow tpApproveFlow) {
		return tpOrderMapper.findTpOrderByConditionNotApproved(tpApproveFlow);
	}

	@Override
	public int deleteApprovalNotSubmit(int id) {
		return tpOrderMapper.deleteById(id);
	}

	@Override
	public int deleteApprovalNotSubmitMany(Integer[] id) {
		if (id != null) {
			for (Integer orderId : id) {
				tpOrderMapper.deleteById(id);
			}
		}
		return 1;
	}
	
	@Override
	public TpOrder selectOrderByApplicationNum(String applicationNum) {
		return tpOrderMapper.findOrderByApplicationNum(applicationNum);
	}

	@Override
	public Boolean saveOrderList(List<String> applicationNumList, List<String> resIdList, List<Integer> userId, Date date) {
		// TODO Auto-generated method stub
		return tpOrderMapper.insertOrderList(applicationNumList, resIdList, userId, date);
	}

	/*
	 * @Override public List<TpOrder> getMap(Integer tpOrderState) {
	 * ///List<TpOrder> tpOrder = list(new QueryWrapper<TpOrder>()); return
	 * tpOrderMapper.selectAllTpOrder(tpOrderState); }
	 */

}
